package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dao.ResultsInfoDao;
import org.example.entity.ResultsInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("IndScoAnalyseService")
public class IndScoAnalyseServiceImpl extends ServiceImpl<ResultsInfoDao, ResultsInfoEntity>
        implements IndScoAnalyseService {
        @Autowired
        private ResultsInfoDao resultsInfoDao;

        @Override
        public Float getTotalScoreByStudent(long studentId) {
            List<ResultsInfoEntity> results = resultsInfoDao.selectList(
                    new QueryWrapper<ResultsInfoEntity>().eq("student_id", studentId));
            return results.stream().map(ResultsInfoEntity::getScore).reduce(0f, Float::sum);
        }

        @Override
        public Float getAverageScoreByExam(long examId) {
            List<ResultsInfoEntity> results = resultsInfoDao.selectList(
                    new QueryWrapper<ResultsInfoEntity>().eq("exam_id", examId));
            return results.stream()
                    .map(ResultsInfoEntity::getScore)
                    .reduce(0f, Float::sum) / results.size();
        }

        @Override
        public Float getHighestScoreByExam(long examId) {
            List<ResultsInfoEntity> results = resultsInfoDao.selectList(
                    new QueryWrapper<ResultsInfoEntity>().eq("exam_id", examId));
            return results.stream()
                    .map(ResultsInfoEntity::getScore)
                    .max(Float::compareTo)
                    .orElse(0f);
        }

        @Override
        public int getPassCountByExam(long examId, Float passingScore) {
            List<ResultsInfoEntity> results = resultsInfoDao.selectList(
                    new QueryWrapper<ResultsInfoEntity>().eq("exam_id", examId));
            return (int) results.stream()
                    .filter(result -> result.getScore() >= passingScore)
                    .count();
        }

        @Override
        public List<ResultsInfoEntity> getStudentScoresByExam(long examId) {
            return resultsInfoDao.selectList(
                    new QueryWrapper<ResultsInfoEntity>().eq("exam_id", examId));
        }

        @Override
        public Map<String, Integer> analyzeStudentAnswers(long studentId, long examId) {
            ResultsInfoEntity result = resultsInfoDao.selectOne(
                    new QueryWrapper<ResultsInfoEntity>().eq("student_id", studentId).eq("exam_id", examId));

            if (result == null || result.getStuAnswer() == null) {
                throw new IllegalArgumentException("No answers found for this student.");
            }

            Map<String, Integer> analysis = new HashMap<>();
            // Assuming we can somehow compare the student's answers to the correct answers.
            // This can be implemented depending on how the answers are stored or retrieved.
            // For now, we'll simulate the analysis logic.
            String correctAnswers = "ABCDE";  // Example: correct answers for this exam.

            String studentAnswers = result.getStuAnswer();
            int correctCount = 0;
            int incorrectCount = 0;

            for (int i = 0; i < studentAnswers.length(); i++) {
                if (studentAnswers.charAt(i) == correctAnswers.charAt(i)) {
                    correctCount++;
                } else {
                    incorrectCount++;
                }
            }

            analysis.put("correct", correctCount);
            analysis.put("incorrect", incorrectCount);
            return analysis;
        }
    }
